package com.nsgf.library.ui.page;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.nsgf.library.BR;
import com.nsgf.library.R;
import com.nsgf.library.data.bean.UserInfo;
import com.nsgf.library.databinding.ActivityProjectOverviewDetailsBinding;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.adapter.XFragmentAdapter;
import com.nsgf.library.ui.page.fragment.ProjectCharacteristicFragment;
import com.nsgf.library.ui.page.fragment.ProjectOverviewBuildingFragment;
import com.nsgf.library.ui.page.fragment.ProjectOverviewRegisterFragment;
import com.nsgf.library.utils.PublicMethodUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * 工程概况
 */
public class ProjectOverviewActivity extends BaseActivity {

    private StateHolder mStates;


    private List<Fragment> fragmentList = new ArrayList<>();

    private String[] mPageTitleList;
    private AccountRequester mAccountRequester;

    private ActivityProjectOverviewDetailsBinding mBinding;

    @Override
    protected void initViewModel() {
        mStates = new StateHolder();
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.activity_project_overview_details, BR.vm, mStates);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getLifecycle().addObserver(mAccountRequester);
        mStates.titleBar.set("工程概况");
        mBinding = (ActivityProjectOverviewDetailsBinding) getBinding();

        // 获取水库信息
        mAccountRequester.reservoirInfo(UserInfo.getInstance().getProjectCode());

        mAccountRequester.getReservoirResult().observe(this, dataResult -> {

            if (dataResult.getResponseCode() == 200) {

                fragmentList.add(ProjectOverviewRegisterFragment.newInstance(dataResult.getData()));
                fragmentList.add(ProjectCharacteristicFragment.newInstance(dataResult.getData()));
                fragmentList.add(ProjectOverviewBuildingFragment.newInstance());
                mPageTitleList = new String[]{"注册登记", "特征参数", "建筑物"};
                mBinding.viewPager.setOffscreenPageLimit(3);
                XFragmentAdapter adapter = new XFragmentAdapter(this, getSupportFragmentManager(), fragmentList, mPageTitleList);
                mBinding.viewPager.setAdapter(adapter);

                PublicMethodUtils.initMagicIndicator(mBinding.magicIndicator, mBinding.viewPager, mPageTitleList, this);
            } else {

                if (401 == dataResult.getResponseCode()) {
                    UserInfo.getInstance().clearUserInfo();
                    finish();
                }
            }
        });
    }
}
